//
//  ScrollImageView.m
//  Qyer
//
//  Created by dlios on 15/10/16.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "ScrollImageView.h"

@implementation ScrollImageView

- (instancetype)initWithFrame:(CGRect)frame{
    a = 0;
    self = [super initWithFrame:frame];
    if (self) {
        _scViewArray = [NSMutableArray array];
        _viewArray = [NSMutableArray array];
        NSString *string = @"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=868842022014563&track_app_version=6.7.1&track_app_channel=xiaomi&track_device_info=hermes&track_os=Android5.0.2&app_installtime=1444898310285&lat=38.883524&lon=121.544823&channel=xiaomi";
        
        [AFN getdataWithString:string block:^(id block) {
            for (NSDictionary *dic in [[block objectForKey:@"data"]objectForKey:@"slide"]) {
                RecommendModel *model = [RecommendModel modelWithDictionary:dic];
                [_scViewArray addObject:model];
            }
            _scView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200 * HEIGHT)];
            [_scView setPagingEnabled:YES];
            [_scView setBounces:YES];
            [_scView setDelegate:self];
            [_scView setShowsHorizontalScrollIndicator:NO];
            [_scView setContentOffset:CGPointMake(0, 0) animated:YES];
            [_scView setBackgroundColor:[UIColor greenColor]];
            [_scView setAutoresizesSubviews:YES];
            [_scView setContentSize:CGSizeMake(_scViewArray.count * SCREEN_WIDTH, 0)];
            [self addSubview:_scView];
            RecommendModel *model = [[RecommendModel alloc] init];
                        _page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 180 * HEIGHT, SCREEN_WIDTH, 20 * HEIGHT)];
            [_page setNumberOfPages:_scViewArray.count];
            [_page setCurrentPageIndicatorTintColor:[UIColor redColor]];
            [_page setPageIndicatorTintColor:[UIColor whiteColor]];
            [_page setCurrentPage:0];
            [self addSubview:_page];
            
            UIImageView *preImage = [[UIImageView alloc] initWithFrame:CGRectMake(-SCREEN_WIDTH, 0, SCREEN_WIDTH, 200 * HEIGHT)];
           RecommendModel *preModel = [_scViewArray lastObject];
            NSString *preString = preModel.photo;
            NSURL *preUrl = [NSURL URLWithString:preString];
            [preImage sd_setImageWithURL:preUrl placeholderImage:[UIImage imageNamed:@"zw.png"]];
            [_scView addSubview:preImage];
            
            UIImageView *hasImage = [[UIImageView alloc] initWithFrame:CGRectMake(_scViewArray.count *SCREEN_WIDTH, 0, SCREEN_WIDTH, 200 * HEIGHT)];
            RecommendModel *hasModel = [_scViewArray firstObject];
            NSString *hasString = hasModel.photo;
            NSURL *hasUrl = [NSURL URLWithString:hasString];
            [hasImage sd_setImageWithURL:hasUrl placeholderImage:[UIImage imageNamed:@"zw.png"]];
            [_scView addSubview:hasImage];
            
            for (int i = 0; i < _scViewArray.count; i++) {
                _scImage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, 200 * HEIGHT)];
                model = [_scViewArray objectAtIndex:i];
                NSURL *url = [NSURL URLWithString:model.photo];
                [_scImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zw.png"]];
                [_scView addSubview:_scImage];
                
                UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 150, SCREEN_WIDTH, 50 * HEIGHT)];
                [backImage setImage:[UIImage imageNamed:@"backViewCell.png"]];
                [_scImage addSubview:backImage];
                
                UILabel *scLable = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i + 10 * WIDTH, 0, SCREEN_WIDTH - 10 * WIDTH, 30 * HEIGHT)];
                [scLable setFont:[UIFont systemFontOfSize:WIDTH * 16.]];
                [scLable setTextColor:[UIColor whiteColor]];
                [scLable setTextAlignment:NSTextAlignmentLeft];
                [scLable setBackgroundColor:[UIColor clearColor]];
                [scLable setText:model.title];
                [_scImage addSubview:scLable];
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, 200 * HEIGHT)];
                [button setBackgroundColor:[UIColor clearColor]];
                [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
                [button setTag:1000 + i];
                [_scView addSubview:button];
            }
            
            for (NSDictionary *dic in [[block objectForKey:@"data"]objectForKey:@"subject"]) {
                RecommendModel *model = [RecommendModel modelWithDictionary:dic];
                [_viewArray addObject:model];
            }
            
            RecommendModel *imageModel = [_viewArray firstObject];
            
            UIView *findView = [[UIView alloc] initWithFrame:CGRectMake(0, 210 * HEIGHT, 365 * WIDTH, 210 * HEIGHT)];
            [findView setBackgroundColor:[NightMode shareNightModeHandle].allViewColor];
            [self addSubview:findView];
            
            UILabel *findLable = [[UILabel alloc] initWithFrame:CGRectMake(5 * WIDTH, 5 * HEIGHT, 365 * WIDTH, 20 * HEIGHT)];
            [findLable setText:@"发现下一站"];
            [findLable setTextAlignment:NSTextAlignmentLeft];
            [findLable setTextColor:[NightMode shareNightModeHandle].wordColor];
            [findLable setFont:[UIFont systemFontOfSize:WIDTH * 14.]];
            [findView addSubview:findLable];
            
            UIImageView *bigImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30 *HEIGHT, 365 * WIDTH, 100 * HEIGHT)];
            [bigImage sd_setImageWithURL:[NSURL URLWithString:imageModel.photo] placeholderImage:[UIImage imageNamed:@"zw.png"]];
            [findView addSubview:bigImage];
            
            _bigButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_bigButton setBackgroundColor:[UIColor clearColor]];
            [_bigButton setFrame:CGRectMake(0 * WIDTH, 3 * HEIGHT, 355 * HEIGHT, 100 * HEIGHT)];
            [_bigButton addTarget:self action:@selector(bigButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [findView addSubview:_bigButton];
            RecommendModel *leftModel = [_viewArray objectAtIndex:1];
            UIImageView *leftIamge = [[UIImageView alloc]initWithFrame:CGRectMake(0 * WIDTH, 135 * HEIGHT, 175 * WIDTH, 70 * HEIGHT)];
            [leftIamge sd_setImageWithURL:[NSURL URLWithString:leftModel.photo] placeholderImage:[UIImage imageNamed:@"zw.png"]];
            [findView addSubview:leftIamge];
            
            _leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0 * WIDTH, 135 * HEIGHT, 175 * WIDTH, 70 * HEIGHT)];
            [_leftButton setBackgroundColor:[UIColor clearColor]];
            [_leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [findView addSubview:_leftButton];
            RecommendModel *rightModel = [_viewArray lastObject];
            UIImageView *rightImage = [[UIImageView alloc] initWithFrame:CGRectMake(190 * WIDTH, 135 * HEIGHT, 175 * WIDTH, 70 * HEIGHT)];
            [rightImage sd_setImageWithURL:[NSURL URLWithString:rightModel.photo] placeholderImage:[UIImage imageNamed:@"zw.ong"]];
            [findView addSubview:rightImage];
            
            _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(190 * WIDTH, 135 * HEIGHT, 175 * WIDTH, 70 * HEIGHT)];
            [_rightButton setBackgroundColor:[UIColor clearColor]];
            [_rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [findView addSubview:_rightButton];

            
        } BLOCK:^(id BLOCK) {
            
        }];
        [self getTimer];
            }

    return self;
}

#pragma mark-右侧图片点击方法实现
- (void)rightButtonClick: (UIButton *)sender{
    RecommendModel *model = [_viewArray lastObject];
    [self.delegate setString:model.url];
}

#pragma mark-左侧图片点击方法实现
- (void)leftButtonClick: (UIButton *)sender{
    RecommendModel *model = [_viewArray objectAtIndex:1];
    [self.delegate setString:model.url];

}
#pragma mark-大图点击方法实现
- (void)bigButtonClick: (UIButton *)sender{
    RecommendModel *model = [_viewArray firstObject];
    [self.delegate setString:model.url];

}



#pragma mark-定时器
- (void)getTimer{
    [NSTimer scheduledTimerWithTimeInterval:2. target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
}
#pragma mark-定时器方法实现
- (void)timerChange{
    [_scView setContentOffset:CGPointMake(SCREEN_WIDTH * a, 0)];
    if (_scView.contentOffset.x > (_scViewArray.count - 1) * SCREEN_WIDTH) {
        [_scView setContentOffset:CGPointMake(0, 0)];
    }
    a++;
    if (a > _scViewArray.count - 1) {
        a = 0;
    }
}

#pragma mark-轮播图点击方法实现
- (void)buttonClick: (UIButton *)sender{
    RecommendModel *model = [_scViewArray objectAtIndex:(sender.tag - 1000)];
    NSString *string = model.url;
    [self.delegate setAString:string];
}

#pragma mark-轮播图滚动代理方法实现
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _page.currentPage = scrollView.contentOffset.x / SCREEN_WIDTH;
    a = scrollView.contentOffset.x / SCREEN_WIDTH;
    if (scrollView.contentOffset.x >(_scViewArray.count - 1) * SCREEN_WIDTH + 80 * WIDTH) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }else if (scrollView.contentOffset.x < -80 * WIDTH){
        [scrollView setContentOffset:CGPointMake((_scViewArray.count - 1) * SCREEN_WIDTH, 0) animated:NO];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
